package com.nari.ncps.screen.service.impl.ne;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nari.ncps.screen.entity.ne.NeOpermonRtPv;
import com.nari.ncps.screen.mapper.ne.NeOpermonRtWindMapper;
import com.nari.ncps.screen.service.ne.NeOpermonRtWindService;
import com.nari.ncps.screen.vo.ne.NePvRealPowerVO;
import com.nari.ncps.screen.vo.ne.NeWindRealPowerVO;
import com.nari.ncps.screen.entity.ne.NeOpermonRtWind;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 风电实时功率表 服务实现类
 * </p>
 *
 * @author xuesong
 * @since 2025-08-23
 */
@Service
public class NeOpermonRtWindServiceImpl extends ServiceImpl<NeOpermonRtWindMapper, NeOpermonRtWind> implements NeOpermonRtWindService {

    @Override
    public List<NeWindRealPowerVO> getWindRealPower(Integer year) {
        String formattedDate = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        // 构建查询条件 - 查询最新一天的24小时数据
        LambdaQueryWrapper<NeOpermonRtWind> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(NeOpermonRtWind::getDelFlag, 0);
//        queryWrapper.like(NeOpermonRtWind::getStatisticTime, "2025-09-29" );
        queryWrapper.like(NeOpermonRtWind::getStatisticTime, formattedDate );
        List<NeOpermonRtWind> latestRecord = this.list(queryWrapper);
        if (latestRecord == null) {
            return new ArrayList<>();
        }
        List<NeWindRealPowerVO> entities = new ArrayList<>();
        for(NeOpermonRtWind data: latestRecord){
            String dateTimeStr = data.getStatisticTime();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            LocalDateTime dateTime = LocalDateTime.parse(dateTimeStr, formatter);
            int newYear = dateTime.getYear();           // 2025
            int month = dateTime.getMonthValue();    // 9
            int day = dateTime.getDayOfMonth();      // 29
            int hour = dateTime.getHour();
            NeWindRealPowerVO vo = new NeWindRealPowerVO();
            vo.setYear(newYear);
            vo.setMonth(month);
            vo.setDay(day);
            vo.setHour(hour);
            vo.setMinute(0);
//            vo.setDate(data.getStatisticTime());
            vo.setRealtimePower(data.getRealtimePower());
            vo.setTheoreticalPower(data.getTheoreticalPower());
            entities.add(vo);
        }
        return entities;
    }
    
    /**
     * 实体类转换为VO
     */
    private NeWindRealPowerVO convertToVO(NeOpermonRtWind entity) {
        NeWindRealPowerVO vo = new NeWindRealPowerVO();
        vo.setYear(entity.getYear());
        vo.setMonth(entity.getMonth());
        vo.setDay(entity.getDay());
        vo.setDate(entity.getDate());
        vo.setHour(entity.getHour() != null ? entity.getHour().intValue() : null);
        vo.setMinute(entity.getMinute() != null ? entity.getMinute().intValue() : null);
        vo.setRealtimePower(entity.getRealtimePower());
        vo.setTheoreticalPower(entity.getTheoreticalPower());
        return vo;
    }

}
